package org.jeecg.modules.cloud_disk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.cloud_disk.entity.Comment;
import org.jeecg.modules.demo.cloud_disk.entity.DocumentInfo;
import org.jeecg.modules.demo.cloud_disk.entity.Upload;
import org.jeecg.modules.demo.cloud_disk.service.ICommentService;
import org.jeecg.modules.demo.cloud_disk.service.IDocumentInfoService;
import org.jeecg.modules.demo.cloud_disk.service.IUploadService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Description: 评论表
 * @Author: jeecg-boot
 * @Date: 2021-09-15
 * @Version: V1.0
 */
@Api(tags = "首页数据接口")
@RestController
@RequestMapping("/cloud_disk/appcontroller")
@Slf4j
public class AppController extends JeecgController<Comment, ICommentService> {
    @Autowired
    private ISysUserService sysUserService;//声明服务

    @Autowired
    private IUploadService iUploadService;


    @GetMapping(value = "/app")
    public Result<?> documentinfo() {
        List<Upload> list = iUploadService.list();
        return Result.OK(list);
    }

    @GetMapping(value = "/app/get")
    public Result<?> documentinfoGet(@RequestParam(name = "id", required = true) String id) {
//        List<Upload> list = iUploadService.list();
//        return Result.OK(list);
        Upload model = iUploadService.getById(id);
        return Result.OK(model);
    }

    @GetMapping(value = "/app/login")
    public Result login(@RequestParam(name = "account") String account, @RequestParam(name = "password") String password) {
        Result result = new Result();
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, account);
        SysUser sysUser = sysUserService.getOne(queryWrapper);
        if (sysUser == null) {
            result.setCode(3);
//            result.setMessage("账号不存在");
            return result;
        }
        //校验用户密码
        String userpassword = PasswordUtil.encrypt(account, password, sysUser.getSalt());
        String syspassword = sysUser.getPassword();
        if (!syspassword.equals(userpassword)) {
            result.setCode(0);
//            result.setMessage("用户名或密码错误");
            return result;
        }
        result.setCode(1);
//        result.setMessage("登陆成功");
        return result;
    }

    @GetMapping(value = "/app/reg")
    public Result reg(@RequestParam(name = "account") String account,
                      @RequestParam(name = "email") String email,
                      @RequestParam(name = "password") String password) {
        Result result = new Result();
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        if (account.equals("")) {
            result.setCode(100);
            return result;
        }
        if (password.equals("")) {
            result.setCode(101);
            return result;
        }
        if (email.equals("")) {
            result.setCode(102);
            return result;
        }
        queryWrapper.eq(SysUser::getUsername, account);
        SysUser sysUser = sysUserService.getOne(queryWrapper);
        if (sysUser == null) {
            //账号不存在才可以注册
            SysUser user = new SysUser();
            user.setCreateTime(new Date());// 设置创建时间
            String salt = oConvertUtils.randomGen(8);
            String passwordEncode = PasswordUtil.encrypt(account, password, salt);
            user.setSalt(salt);
            user.setUsername(account);
            user.setRealname(account);
            user.setPassword(passwordEncode);
            user.setEmail(email);
            user.setStatus(CommonConstant.USER_UNFREEZE);
            user.setDelFlag(CommonConstant.DEL_FLAG_0);
            user.setActivitiSync(CommonConstant.ACT_SYNC_0);
            sysUserService.addUserWithRole(user, "ee8626f80f7c2619917b6236f3a7f02b");//默认临时角色 test
            result.setCode(1);
            result.success("注册成功");
            return result;
        }
        result.setCode(0);//0账号存在，1账号不存在
        return result;
    }


    //    @GetMapping(value = "/get")
//    public Result<?> getDate() {
//        //去视图里面读取数据，就要调用Mapper,这个架构就是用service数据来调用
////       List<DocumentInfo> list = iDocumentInfoService.list();
//       return Result.OK(list);
//    }
    @Autowired
    private ISysBaseAPI isysBaseAPI;

    @RequestMapping("/send")
    public void send() {
        isysBaseAPI.sendEmailMsg("1271013637@qq.com", "你猜猜这是什么", "我用的是小赫隐形输入法");
    }

    @RequestMapping("/resetPwd")
    public Result restPwd(@RequestParam(name = "email") String email) {
        Result result = new Result();
        //根据用户输入的邮箱，查询这个用户是否存在
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getEmail, email);
        SysUser sysUser = sysUserService.getOne(queryWrapper);
        if (sysUser != null) {
            //如果它是空，就说明它存在,然后重置密码
            String salt = oConvertUtils.randomGen(8);
            String passwordEncode = PasswordUtil.encrypt(sysUser.getUsername(), "123456", salt);//生成新密码
            sysUser.setPassword(passwordEncode);//更新密码
            sysUser.setSalt(salt);//更新密码盐
            sysUserService.saveOrUpdate(sysUser);//保存到数据库
            result.setCode(200);
            result.setMessage("密码修改成功，请登录邮箱查看新密码");
            isysBaseAPI.sendEmailMsg(email,"密码重置成功，请查看","新密码是"+123456);
            return result;
        }else{
            result.setCode(201);
            result.setMessage("该邮箱未注册");
            //如果存在就重置密码，发送新密码到用户的邮箱
        }
        result.setCode(500);
        result.setMessage("注册异常");
        return result;
    }

}
